package com.heer.gymnasium.base;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class BaseRefreshFragment extends BaseFragment implements SwipyRefreshLayout.OnRefreshListener {

    protected TextView tvDesc;
    protected Button btnRefresh;
    protected LinearLayout layoutNoData;
    protected FrameLayout layoutContent;
    protected FrameLayout layoutBase;

    protected int pageNo = 1;

    @Override
    protected int setConvertView() {
        return R.layout.layout_base_no_data;
    }

    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        if (direction == SwipyRefreshLayoutDirection.TOP) {
            pageNo = 1;
            loadData(false);
        } else if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
            pageNo++;
            loadData(true);
        }
    }

    public View addContentView(View root, int resource) {
        initBaseRefreshView(root);
        LayoutInflater inflater = LayoutInflater.from(root.getContext());
        View view = inflater.inflate(resource, null);
        if (layoutContent != null) {
            layoutContent.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        return view;
    }

    private void initBaseRefreshView(View view) {
        tvDesc = (TextView) view.findViewById(R.id.tv_desc);
        btnRefresh = (Button) view.findViewById(R.id.btn_refresh);
        layoutNoData = (LinearLayout) view.findViewById(R.id.layout_no_data);
        layoutBase = (FrameLayout) view.findViewById(R.id.layout_base);
        layoutContent = (FrameLayout) view.findViewById(R.id.layout_content);
    }

    protected void toLogin(){
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
}
